package com.sunb2b.dao;

import java.util.List;
import org.hibernate.LockMode;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import com.sunb2b.entity.*;
/**
 * A data access object (DAO) providing persistence and search support for
 * CompanyMember entities. Transaction control of the save(), update() and
 * delete() operations can directly support Spring container-managed
 * transactions or they can be augmented to handle user-managed Spring
 * transactions. Each of these methods provides additional information for how
 * to configure it for the desired type of transaction control.
 * 
 * @see com.sunb2b.dao.CompanyMember
 * @author MyEclipse Persistence Tools
 */

public class CompanyMemberDAO extends HibernateDaoSupport {
	private static final Logger log = LoggerFactory
			.getLogger(CompanyMemberDAO.class);
	// property constants
	public static final String LEGAL_PERSION = "legalPersion";
	public static final String TYPE = "type";
	public static final String WEBSITE = "website";
	public static final String REG_NO = "regNo";
	public static final String BIZ = "biz";
	public static final String STATUS = "status";

	protected void initDao() {
		// do nothing
	}

	public void save(CompanyMember transientInstance) {
		log.debug("saving CompanyMember instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CompanyMember persistentInstance) {
		log.debug("deleting CompanyMember instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CompanyMember findById(java.lang.Integer id) {
		log.debug("getting CompanyMember instance with id: " + id);
		try {
			CompanyMember instance = (CompanyMember) getHibernateTemplate()
					.get("com.sunb2b.dao.CompanyMember", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CompanyMember instance) {
		log.debug("finding CompanyMember instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CompanyMember instance with property: "
				+ propertyName + ", value: " + value);
		try {
			String queryString = "from CompanyMember as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByLegalPersion(Object legalPersion) {
		return findByProperty(LEGAL_PERSION, legalPersion);
	}

	public List findByType(Object type) {
		return findByProperty(TYPE, type);
	}

	public List findByWebsite(Object website) {
		return findByProperty(WEBSITE, website);
	}

	public List findByRegNo(Object regNo) {
		return findByProperty(REG_NO, regNo);
	}

	public List findByBiz(Object biz) {
		return findByProperty(BIZ, biz);
	}

	public List findByStatus(Object status) {
		return findByProperty(STATUS, status);
	}

	public List findAll() {
		log.debug("finding all CompanyMember instances");
		try {
			String queryString = "from CompanyMember";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CompanyMember merge(CompanyMember detachedInstance) {
		log.debug("merging CompanyMember instance");
		try {
			CompanyMember result = (CompanyMember) getHibernateTemplate()
					.merge(detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CompanyMember instance) {
		log.debug("attaching dirty CompanyMember instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CompanyMember instance) {
		log.debug("attaching clean CompanyMember instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CompanyMemberDAO getFromApplicationContext(
			ApplicationContext ctx) {
		return (CompanyMemberDAO) ctx.getBean("CompanyMemberDAO");
	}
}